﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using AMS.Core.Data;
using AMS.Core.Services;
using AMS.Core;
using AMS.DD35.DbAccess;

namespace AMS.DD35.ServiceLib.Services
{
    public class LoginService : ILoginService
    {
        private ISessionService _session;
        private ILoginConnector _login;

        public ISessionService SessionService
        {
            get { return _session; }
        }

        public LoginService(ILoginConnector loginConnector, ISessionService sessionService)
        {
            this._login = loginConnector;
            this._session = sessionService;
        }

        public object AttemptLogin(LoginData loginData)
        {
            if (!_login.VerifyUsernamePassword(loginData))
                ExceptionHandler.InvalidCredentialsException();

            if (_session.HasSession(loginData.Username))
                ExceptionHandler.Exception(String.Format("{0} is already logged in", loginData.Username));

            return _session.StartSession(loginData.Username);
        }

        public void Logout(object sessionVariable)
        {
            if (_session.IsValidSession(sessionVariable))
            {
                _session.EndSession(sessionVariable);
            }
        }
    }
}
